package com.wowangz.cms.user.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.user.pojo.User;

/**
 * ServiceImpl.
 * 
 * @author auto
 */
public class UserService extends BaseService {

	private Logger logger = LoggerFactory.getLogger(UserService.class);

	public void add(User user) throws ServiceException {
		logger.debug("in add user");
		try {
			this.getSqlSession().insert("com.wowangz.cms.user.dao.IUserDao.insert", user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}

	}
	public void modify(User user) throws ServiceException {
		logger.debug("modify user passwod");
		try {
			this.getSqlSession().insert("com.wowangz.cms.user.dao.IUserDao.modify", user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
		
	}

	public void delete(String ids) throws ServiceException {
		if (Strings.isNullOrEmpty(ids)) {
			return;
		}
		String[] temIds = ids.split(",");
		for (String id : temIds) {
			this.getSqlSession().delete("com.wowangz.cms.user.dao.IUserDao.deleteUser", id);
			try {
				this.getSqlSession().delete("com.wowangz.cms.user.dao.IUserDao.deleteUser", id);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
		}
	}

	public void update(User user) throws ServiceException {
		try {
			this.getSqlSession().update("com.wowangz.cms.user.dao.IUserDao.updateUser", user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}

	}

	public User findUserById(String id) throws ServiceException {
		User user = null;
		try {
			user = this.getSqlSession().selectOne("com.wowangz.cms.user.dao.IUserDao.findUserById", id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
		return user;
	}

	public List<User> query(User user) throws ServiceException {
		List<User> list = null;
		try {
			list = this.getSqlSession().selectList("com.wowangz.cms.user.dao.IUserDao.queryUser", user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
		return list;

	}

	/**
	 * 校验用户名重复
	 * 
	 * @param map
	 * @return
	 */

	public List<User> toCheckUser(User user) throws ServiceException {
		List<User> list = null;
		try {
			list = this.getSqlSession().selectList("com.wowangz.cms.user.dao.IUserDao.toCheckUser", user);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
		return list;

	}

	/**
	 * 验证信息
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public boolean validationUser(HashMap<String, Object> map) throws Exception {
		Long countNum = null;
		try {
			countNum = this.getSqlSession().selectOne("com.wowangz.cms.user.dao.IUserDao.validationUser", map);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
		if (countNum != 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 分页查询总条数
	 * 
	 * @param map
	 * @return
	 */
	public long findResultCount(HashMap<String, Object> map) throws ServiceException {
		try {
			Long countNum = this.getSqlSession().selectOne("com.wowangz.cms.user.dao.IUserDao.queryUserCount", map);
			return countNum;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	public long queryCheckUserCount(Map<String, Object> map) throws ServiceException {
		try {
			Long countNum = this.getSqlSession().selectOne("com.wowangz.cms.user.dao.IUserDao.queryCheckUserCount", map);
			return countNum;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}

	/**
	 * 查询记录集合
	 * 
	 * @param map
	 * @return
	 */
	public List<User> findResultList(HashMap<String, Object> map) throws ServiceException {
		List<User> list = null;
		try {
			list = this.getSqlSession().selectList("com.wowangz.cms.user.dao.IUserDao.queryUserPage", map);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}

		return list;
	}

	/**
	 * 用户复核
	 * 
	 * @param map
	 * @return
	 */
	public void checkUser(String ids) throws ServiceException {
		if (Strings.isNullOrEmpty(ids)) {
			return;
		}
		String[] temIds = ids.split(",");
		User user = new User();
		user.setCheckstatus(SysConstant.CheckStatus.CHECKED);
		for (String id : temIds) {
			user.setId(Long.valueOf(id));
			try {
				this.getSqlSession().delete("com.wowangz.cms.user.dao.IUserDao.checkUser", user);
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServiceException(e.getMessage());
			}
		}
		return;
	}
	
	
	public User queryUserById(String id) throws ServiceException {
		try {
			User user = getSqlSession().selectOne("com.wowangz.cms.user.dao.IUserDao.queryUserById", id);
			return user;
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
}
